package com.ebelter.btcomlib.utils;


import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.lang.ref.WeakReference;

public class BaseHandle {


    private MainHandle mMainHandle;


    public void post(Runnable r) {
        if (mMainHandle != null) mMainHandle.post(r);
    }
    public void postDelayed(Runnable r,long delayMillis) {
        if (mMainHandle != null) mMainHandle.postDelayed(r,delayMillis);
    }

    public BaseHandle(Object... objects) {
        Object object;
        if (objects == null || objects.length == 0) {
            object = this;
        } else {
            object = objects[0];
        }
        mMainHandle = new MainHandle(object);
    }

    @SuppressLint("HandlerLeak")
    class MainHandle extends Handler {
        private WeakReference<Object> weakReference;

        MainHandle(Object obj) {
            super(Looper.getMainLooper());
            this.weakReference = new WeakReference<>(obj);
        }

        @Override
        public void handleMessage(Message msg) {
            if (weakReference.get() != null) {
                if (msgCallBack!=null){msgCallBack.handleMsg(msg);}
                handleMsg(msg);
            }
        }
    }

    public boolean sendEmptyMessage(int what) {
        return sendEmptyMessageDelayed(what, 0);
    }

    public boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        return mMainHandle != null && mMainHandle.sendEmptyMessageDelayed(what, delayMillis);
    }

    public boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
        return mMainHandle != null && mMainHandle.sendEmptyMessageAtTime(what, uptimeMillis);
    }

    public boolean sendMessageDelayed(Message msg, long delayMillis) {
        return mMainHandle != null && mMainHandle.sendMessageDelayed(msg, delayMillis);
    }

    public boolean sendMessage(Message msg) {
        return sendMessageDelayed(msg, 0);
    }

    public boolean sendMessageValue(int what, int... values) {

        if (values == null || values.length == 0) {
            return false;
        }
        Message msg = Message.obtain();
        msg.what = what;
        if (values.length == 1) {
            msg.arg1 = values[0];
        } else if (values.length == 2) {
            msg.arg2 = values[1];
        }
        return sendMessage(msg);
    }


    public boolean sendMessageObj(int what, Object obj) {
        if (obj == null) return false;
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = obj;
        sendMessage(msg);
        return true;
    }


    public void removeMessages(int what) {
        if (mMainHandle != null) mMainHandle.removeMessages(what);
    }

    public void handleMsg(Message msg) {


    }

    public void exit(){
        removeCallbacksAndMessages();
        msgCallBack=null;
        mMainHandle=null;
    }

    public void removeCallbacksAndMessages() {
        if (mMainHandle != null) mMainHandle.removeCallbacksAndMessages(null);
    }

    private IHandleMsgCallBack msgCallBack;

    public void setMsgCallBack(IHandleMsgCallBack msgCallBack) {
        this.msgCallBack = msgCallBack;
    }

    public interface IHandleMsgCallBack{
        void handleMsg(Message msg);
    }
}
